package cn.tedu.hotel.serve.controller;


import cn.tedu.hotel.serve.pojo.dto.HotelScoreDTO;
import cn.tedu.hotel.serve.pojo.entity.Theme;
import cn.tedu.hotel.serve.pojo.vo.HotelScoreVO;
import cn.tedu.hotel.serve.security.UserLoginPrincipal;
import cn.tedu.hotel.serve.service.IHotelScoreService;
import cn.tedu.hotel.serve.service.IHotelService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hotelScores")
@Api(tags = "04. 酒店分数管理模块")
@Validated
public class HotelScoreController {

    @Autowired
    IHotelScoreService hotelScoreService;

    @PostMapping("/user/addHotelScore")
    @ApiOperation("用户进行评分")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addHotelScore(
            @Validated HotelScoreDTO hotelScoreDTO,
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal
    ){
        hotelScoreDTO.setUserId(userLoginPrincipal.getId());
        hotelScoreDTO.setUsername(userLoginPrincipal.getUsername());
        log.debug("开始处理【前台用户进行评分】的请求",hotelScoreDTO);
        hotelScoreService.addHotelScore(hotelScoreDTO);
        return JsonResult.ok();
    }

    @GetMapping("/user/getScore/{hotelId:[0-9]+}")
    @ApiOperation("获取单个用户对酒店的分数")
    @ApiOperationSupport(order = 400)
    public JsonResult<HotelScoreVO> getHotelScoreByUserIdAndHotelId(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal,
            @PathVariable("hotelId") @Range(min = 1,message = "请提交有效值") Long hotelId
    ){
        log.debug("开始处理【前台根据酒店ID：{}，用户ID：{}，查询用户对酒店的评分】的业务，参数：{}",
                hotelId,userLoginPrincipal.getId());
        HotelScoreVO hotelScoreVO =
                hotelScoreService.getHotelScoreByUserIdAndHotelId(userLoginPrincipal.getId(), hotelId);
        return JsonResult.ok(hotelScoreVO);
    }

    @PostMapping("/admin/{id:[0-9]+}/deleteScoreById")
    @ApiOperation("后台根据评分ID删除数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    public JsonResult<Void> deleteScoreById(
            @PathVariable @Range(min = 1,message = "请提交有效的值")Long id
    ){
        log.debug("开始处理【后台根据评分ID：{}删除数据】的请求",id);
        hotelScoreService.deleteScoreById(id);
        return JsonResult.ok();
    }

    @GetMapping("/admin/hotelScore")
    @ApiOperation("后台返回评分列表数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<HotelScoreVO>> getAdminUserScoreList(){
        log.debug("开始处理【后台返回评分列表数据】的请求");
        List<HotelScoreVO> list = hotelScoreService.getAdminUserScoreList();
        log.debug("数据：{}",list);
        return JsonResult.ok(list);
    }

}
